    ; shell程序
    ; @author: Milon
    ; @date: 2024/04/30

section shell_header
    length  dq  shell_end                           ; 0x00 shell程序长度
    entry   dq  start                               ; 0x08 shell入口点偏移量
    linear  dq  0                                   ; 0x10 shell起始线性地址

section shell_data
    shell_msg    times 128 db 0

    msg0         db "Thread ", 0
    tid          times 32 db 0                      ; 线程ID的文本
    msg1         db " <OS SHELL> on CPU ", 0
    pcpu         times 32 db 0                      ; 处理器编号的文本
    msg2         db " -", 0

    time_buff    times 32 db 0                      ; 当前时间的文本


section shell_code
    %include "./common/user_static64.asm"

    bits 64

main:
    ; 创建进程
    mov r8, 100
    mov rax, 3
    syscall

    ; 获取可用屏幕行
    mov rax, 0
    syscall
    mov dl, 0
    mov r9b, 0x5f

    mov r12, [rel linear]
_time:
    lea rbx, [r12 + time_buff]
    mov rax, 1
    syscall

    ; 获取当前处理器编号
    mov rax, 6
    syscall
    mov r8, rax
    lea rbx, [r12 + pcpu]
    call bin64_to_dec

    ; 获取当前线程编号
    mov rax, 8
    syscall
    mov r8, rax
    lea rbx, [r12 + tid]
    call bin64_to_dec

    lea rdi, [r12 + shell_msg]
    mov byte [rdi], 0

    lea rsi, [r12 + msg0]
    call strcat                 ; 字符串连接

    lea rsi, [r12 + tid]
    call strcat                 ; 字符串连接

    lea rsi, [r12 + msg1]
    call strcat                 ; 字符串连接

    lea rsi, [r12 + pcpu]
    call strcat                 ; 字符串连接

    lea rsi, [r12 + msg2]
    call strcat                 ; 字符串连接

    lea rsi, [r12 + time_buff]
    call strcat                 ; 字符串连接

    mov rbx, rdi
    mov rax, 2
    syscall

    ; 休眠550ms
    push rdx
    mov rdx, 10
    mov rax, 15
    syscall
    pop rdx

    jmp _time

start:
    call main

shell_end:
